package cn.iocoder.yudao.module.hs.service.receiveorderdetail;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.hs.controller.admin.receiveorderdetail.vo.ReceiveOrderDetailPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.receiveorderdetail.vo.ReceiveOrderDetailRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.receiveorderdetail.vo.ReceiveOrderDetailSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.receiveorderdetail.ReceiveOrderDetailDO;
import cn.iocoder.yudao.module.hs.dal.mysql.receiveorderdetail.ReceiveOrderDetailMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.RECEIVE_ORDER_DETAIL_NOT_EXISTS;

/**
 * 收货单明细 Service 实现类
 *
 * @author 航盛
 */
@Service
@Validated
public class ReceiveOrderDetailServiceImpl implements ReceiveOrderDetailService {

    @Resource
    private ReceiveOrderDetailMapper receiveOrderDetailMapper;

    @Override
    public Long createReceiveOrderDetail(ReceiveOrderDetailSaveReqVO createReqVO) {
        // 插入
        ReceiveOrderDetailDO receiveOrderDetail = BeanUtils.toBean(createReqVO, ReceiveOrderDetailDO.class);
        receiveOrderDetailMapper.insert(receiveOrderDetail);
        // 返回
        return receiveOrderDetail.getId();
    }

    @Override
    public void updateReceiveOrderDetail(ReceiveOrderDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateReceiveOrderDetailExists(updateReqVO.getId());
        // 更新
        ReceiveOrderDetailDO updateObj = BeanUtils.toBean(updateReqVO, ReceiveOrderDetailDO.class);
        receiveOrderDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteReceiveOrderDetail(Long id) {
        // 校验存在
        validateReceiveOrderDetailExists(id);
        // 删除
        receiveOrderDetailMapper.deleteById(id);
    }

    private void validateReceiveOrderDetailExists(Long id) {
        if (receiveOrderDetailMapper.selectById(id) == null) {
            throw exception(RECEIVE_ORDER_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public ReceiveOrderDetailDO getReceiveOrderDetail(Long id) {
        return receiveOrderDetailMapper.selectById(id);
    }

    @Override
    public PageResult<ReceiveOrderDetailRespVO> getReceiveOrderDetailPage(ReceiveOrderDetailPageReqVO pageReqVO) {
        Page<ReceiveOrderDetailRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<ReceiveOrderDetailRespVO> receiveOrderDetailRespVOIPage = receiveOrderDetailMapper.selectPageNotReceiveRecord(page, pageReqVO);
        PageResult<ReceiveOrderDetailRespVO> pageResult = new PageResult<>();
        pageResult.setList(receiveOrderDetailRespVOIPage.getRecords());
        pageResult.setTotal(receiveOrderDetailRespVOIPage.getTotal());
        return pageResult;
    }

    @Override
    public ReceiveOrderDetailDO getReceiveOrderDetail(String materialCode) {
        return receiveOrderDetailMapper.selectOne(ReceiveOrderDetailDO::getMaterialCode, materialCode);
    }

    @Override
    public List<ReceiveOrderDetailDO> getReceiveOrderDetailList(String trayCode) {
        return receiveOrderDetailMapper.selectList(ReceiveOrderDetailDO::getTrayCode, trayCode);
    }

}